//
//  PTFunDetailView.swift
//  PointTest28
//
//  Created by miaokii on 2025/7/19.
//

import SwiftUI

struct FunDetailModel {
    var subTitle: String
    var subDescs: [String]
}

// 功能能说明
struct PTFunDetailView: View {
    
    var ptFunc: any PTFunc
    
    @State private var funcName = ""
    @State private var funcDes: [String]?
    @State private var groups: [FunDetailModel]?
    
    var body: some View {
        ScrollView(content: {
            VStack(alignment: .leading, spacing: 10) {
                HStack{
                    Text("测试项名称：")
                    Text(funcName)
                        .foregroundColor(.gray)
                    Spacer()
                }
                Text("测试项功能：")
                if let funcDes = funcDes {
                    VStack(alignment: .leading) {
                        ForEach(0..<funcDes.count, id: \.self) { idx in
                            HStack(alignment: .top) {
                                Text("\(idx+1).")
                                Text(funcDes[idx])
                            }
                            .font(.system(size: 14, design: .monospaced))
                            .foregroundColor(.gray)
                            .padding(.top, 1)
                        }
                    }
                }
                else if let groups = groups {
                    VStack(alignment: .leading) {
                        ForEach(0..<groups.count, id: \.self) { idx in
                            VStack(alignment: .leading)  {
                                Text((idx+1).spell + ". " + groups[idx].subTitle)
                                    .font(.system(size: 14, weight: .semibold, design: .monospaced))
                                    .foregroundColor(.gray)
                                    .padding(.bottom, 2)
                                
                                ForEach(0..<groups[idx].subDescs.count, id: \.self) { subIdx in
                                    HStack(alignment: .top) {
                                        Text("\(subIdx+1).")
                                            .padding(.leading, 10)
                                        Text(groups[idx].subDescs[subIdx])
                                        Spacer()
                                    }
                                    .font(.system(size: 14, design: .monospaced))
                                    .foregroundColor(.gray)
                                    .padding(.bottom, 1)
                                }
                            }
                            .padding(.bottom, 5)
                        }
                    }
                }
                else {
                    Text("系统暂未开放此功能API，开发功能受限")
                        .font(.system(size: 14, design: .monospaced))
                        .foregroundColor(.gray)
                        .padding(.top, 1)
                }
            }
            .padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
        })
        .onAppear{
            funcName = ptFunc.title
            funcDes = ptFunc.desc
            groups = ptFunc.groupDesc
        }
    }
}

#Preview {
    PTFunDetailView(ptFunc: PTFuncRequireOne.tel)
}

extension Int {
    var spell: String {
        let formatter = NumberFormatter()
        formatter.numberStyle = .spellOut
        formatter.locale = .init(identifier: "zh_CN")
        let number = NSNumber.init(integerLiteral: self)
        return formatter.string(from: number) ?? "\(self)"
    }
}
